//
//  XCVisitorView.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/6.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit
import SnapKit

//声明协议
@objc protocol XCVisitorViewDelegate: NSObjectProtocol{

    //定义协议方法
  optional func userWillLogin()
    
  optional func userWillRegist()
    
}


class XCVisitorView: UIView {
    
    

    //定义代理属性
    weak var delegate:XCVisitorViewDelegate?
    
    //设置不同的visitor界面
    func setVisitor(imageName:String?, text:String){
        tipLabel.text = text

        if imageName == nil{
            
            //如果name为空  那么说明是第一个主界面 这时 只需要让动画转动就可以了
            startAnimation()

        
        } else {
        
            //如果name不为空  则说明是其他界面  这时在设置标签的内容和图片的内容
            
            circle.image = UIImage(named: imageName!)
            
            //隐藏大房子以及mask
            home.hidden = true
            maskImage.hidden = true

            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        //设置背景色
        let scal:CGFloat = 236.0 / 255.0
        self.backgroundColor = UIColor(red: scal, green: scal, blue: scal, alpha: 1)
        
        addChild()
    }
    
    //让转盘转起来
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        
        circle.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //设置子控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        circle.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        maskImage.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circle.snp_center)
        }
        
        home.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(circle.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(230)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(tipLabel.snp_bottom).offset(10)
            make.leading.equalTo(tipLabel.snp_leading)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        registBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(tipLabel.snp_bottom).offset(10)
            make.trailing.equalTo(tipLabel.snp_trailing)
            make.width.equalTo(100)
            make.height.equalTo(30)

        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //添加控件
    func addChild(){
        
        addSubview(circle)
        addSubview(maskImage)
        addSubview(home)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registBtn)
        
        
    
    }
    
    //懒加载控件
    lazy var circle : UIImageView = {
        
        let circleView = UIImageView()
        
        circleView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        circleView.sizeToFit()
        
        
        return circleView
    }()
    
    //懒加载遮挡视图
    lazy var maskImage : UIImageView = {
        
        let mask = UIImageView()
        
        mask.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return mask
    }()
    
    //懒加载小房子
    lazy var home : UIImageView = {
        
        let homeView = UIImageView()
        
        homeView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return homeView
    }()

    
    //懒加载lable
    lazy var tipLabel : UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人  看看有什么惊喜"
        
        label.textColor = UIColor.grayColor()

        label.textAlignment = .Center
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //懒加载登录按钮
    lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        let image = UIImage(named: "common_button_white_disable")
        let imageW = Int((image?.size.width)! * 0.5)
        let imageH = Int((image?.size.height)!*0.5)
        
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: "loginClick", forControlEvents: .TouchUpInside)
        
        
        return btn
    }()
    
    //懒加载注册按钮
    lazy var registBtn : UIButton = {
        

        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        let image = UIImage(named: "common_button_white_disable")
        let imageW = Int((image?.size.width)! * 0.5)
        let imageH = Int((image?.size.height)!*0.5)


        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
        btn.sizeToFit()
        
         btn.addTarget(self, action: "registClick", forControlEvents: .TouchUpInside)
        
        return btn

        
    }()
    
    
    //点击登录按钮的方法
    func loginClick(){
        
        //第一个问号-->是否设置了代理 若没设置代理  则返回nil  不执行
        //第二个问号-->代理的方法是否可以实现若可以  则执行  若没实现  则返回nil
        
        delegate?.userWillLogin?()
        
    }
    
    //点击注册调用的方法
    func registClick(){
    
        delegate?.userWillRegist?()
    }
}



